package org.xuk.spring.ws.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class CommonConfig {

    @Value("${swagger.enable:false}")
    private boolean swaggerEnabled;

    @Bean
    public Gson gson() {
        return new GsonBuilder().create();
    }

    @Bean
    public Docket docket() {
        ApiInfo build = new ApiInfoBuilder().title("websocket-gateway")
                .contact(new Contact("xuk", "www.test.hello", null))
                .version("1.0")
                .description("WebSocket gateway")
                .build();

        return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true)
                .apiInfo(build)
                .enable(swaggerEnabled)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
}
